Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 555)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.94934 12.94971 12.95011 12.95051 12.95092 12.95132 12.95173 12.95212
##   [9] 12.95251 12.95287 12.95321 12.95352 12.95380 12.95404 12.95425 12.95440
##  [17] 12.95450 12.95455 12.95453 12.95445 12.95430 12.95408 12.95377 12.95338
##  [25] 12.95290 12.95232 12.95165 12.95087 12.94999 12.94899 12.94789 12.94669
##  [33] 12.94540 12.94402 12.94256 12.94104 12.93945 12.93780 12.93610 12.93436
##  [41] 12.93259 12.93079 12.92896 12.92712 12.92527 12.92342 12.92158 12.91975
##  [49] 12.91794 12.91615 12.91441 12.91270 12.91104 12.90943 12.90789 12.90641
##  [57] 12.90502 12.90351 12.90172 12.89965 12.89731 12.89472 12.89189 12.88883
##  [65] 12.88554 12.88205 12.87836 12.87449 12.87044 12.86623 12.86187 12.85737
##  [73] 12.85274 12.84799 12.84314 12.83819 12.83316 12.82806 12.82289 12.81768
##  [81] 12.81243 12.80716 12.80187 12.79658 12.79129 12.78603 12.78079 12.77560
##  [89] 12.77046 12.76539 12.76040 12.75549 12.75068 12.74598 12.74140 12.73696
##  [97] 12.73266 12.72852 12.72454 12.72075 12.71714 12.71374 12.71055 12.70758
## [105] 12.70485 12.70237 12.70015 12.69819 12.69652 12.69514 12.69407 12.69331
## [113] 12.69288 12.69275 12.69290 12.69332 12.69401 12.69496 12.69619 12.69767
## [121] 12.69942 12.70144 12.70370 12.70623 12.70901 12.71204 12.71532 12.71885
## [129] 12.72263 12.72665 12.73092 12.73542 12.74016 12.74514 12.75036 12.75581
## [137] 12.76148 12.76739 12.77353 12.77988 12.78799 12.79905 12.81261 12.82820
## [145] 12.84539 12.86369 12.88265 12.90182 12.92074 12.93894 12.95597 12.97137
## [153] 12.98468 12.99544 13.00549 13.01691 13.02959 13.04345 13.05837 13.07427
## [161] 13.09103 13.10857 13.12678 13.14556 13.16482 13.18445 13.20436 13.22444
## [169] 13.24460 13.26474 13.28476 13.30455 13.32403 13.34308 13.36162 13.37954
## [177] 13.39674 13.41313 13.42860 13.44305 13.45639 13.46851 13.47933 13.48873
## [185] 13.49662 13.50290 13.50746 13.51022 13.51107 13.51043 13.50880 13.50621
## [193] 13.50268 13.49825 13.49294 13.48678 13.47979 13.47201 13.46347 13.45419
## [201] 13.44419 13.43351 13.42218 13.41022 13.39766 13.38453 13.37085 13.35666
## [209] 13.34198 13.32684 13.30940 13.28816 13.26366 13.23644 13.20707 13.17607
## [217] 13.14400 13.11140 13.07882 13.04680 13.01590 12.98664 12.95959 12.93528
## [225] 12.91121 12.88464 12.85577 12.82482 12.79200 12.75751 12.72156 12.68436
## [233] 12.64613 12.60706 12.56737 12.52726 12.48695 12.44663 12.40653 12.36685
## [241] 12.32780 12.28959 12.25242 12.21650 12.18204 12.14926 12.11836 12.08954
## [249] 12.06302 12.03901 12.01771 11.99933 11.98367 11.97028 11.95900 11.94971
## [257] 11.94225 11.93649 11.93227 11.92945 11.92789 11.92744 11.92797 11.92932
## [265] 11.93135 11.93392 11.93688 11.94009 11.94340 11.94668 11.94977 11.95253
## [273] 11.95482 11.95649 11.95740 11.95741 11.95636 11.95412 11.95055 11.94549
## [281] 11.94004 11.93534 11.93133 11.92794 11.92511 11.92276 11.92082 11.91925
## [289] 11.91796 11.91688 11.91597 11.91514 11.91433 11.91347 11.91250 11.91135
## [297] 11.90995 11.90824 11.90616 11.90362 11.90057 11.89695 11.89267 11.88769
## [305] 11.88193 11.87532 11.86779 11.85929 11.84850 11.83444 11.81756 11.79830
## [313] 11.77711 11.75444 11.73073 11.70642 11.68197 11.65781 11.63439 11.61216
## [321] 11.59157 11.57305 11.55705 11.54402 11.53020 11.51206 11.49039 11.46600
## [329] 11.43970 11.41228 11.38455 11.35730 11.33134 11.30748 11.28650 11.26922
## [337] 11.25643 11.24895 11.24493 11.24203 11.24021 11.23946 11.23976 11.24109
## [345] 11.24343 11.24675 11.25104 11.25628 11.26245 11.26953 11.27750 11.28633
## [353] 11.29602 11.30653 11.31785 11.32997 11.34285 11.35648 11.37085 11.38592
## [361] 11.40168 11.41811 11.43520 11.45291 11.47124 11.49015 11.51106 11.53518
## [369] 11.56220 11.59181 11.62372 11.65763 11.69321 11.73019 11.76823 11.80706
## [377] 11.84635 11.88581 11.92513 11.96401 12.00214 12.03923 12.07496 12.10903
## [385] 12.14114 12.17098 12.19825 12.22664 12.25950 12.29602 12.33541 12.37685
## [393] 12.41955 12.46269 12.50549 12.54712 12.58679 12.62370 12.65703 12.68599
## [401] 12.70977 12.73041 12.75045 12.76987 12.78866 12.80679 12.82425 12.84101
## [409] 12.85706 12.87238 12.88694 12.90073 12.91372 12.92591 12.93725 12.94775
## [417] 12.95737 12.96609 12.97391 12.98079 12.98672 12.99167 12.99564 12.99859
## [425] 13.00051 13.00137 13.00117 12.99987 12.99746 12.99276 12.98482 12.97395
## [433] 12.96050 12.94477 12.92711 12.90782 12.88725 12.86571 12.84353 12.82103
## [441] 12.79854 12.77639 12.75490 12.73439 12.71519 12.69764 12.68204 12.66873
## [449] 12.65461 12.63678 12.61587 12.59250 12.56728 12.54083 12.51379 12.48676
## [457] 12.46038 12.43526 12.41203 12.39130 12.37370 12.35985 12.34808 12.33635
## [465] 12.32468 12.31310 12.30164 12.29031 12.27916 12.26819 12.25746 12.24697
## [473] 12.23675 12.22684 12.21726 12.20803 12.19919 12.19076 12.18276 12.17523
## [481] 12.16819 12.16167 12.15569 12.15029 12.14548 12.14130 12.13777 12.13492
## [489] 12.13277 12.13136 12.13053 12.13013 12.13016 12.13063 12.13156 12.13295
## [497] 12.13481 12.13716 12.14000 12.14333 12.14718 12.15155 12.15644 12.16188
## [505] 12.16786 12.17440 12.18151 12.18919 12.19746 12.20632 12.21579 12.22587
## [513] 12.23658 12.24792 12.25990 12.27254 12.28583 12.29980 12.31445 12.32979
## [521] 12.34575 12.36229 12.37939 12.39705 12.41528 12.43408 12.45344 12.47336
## [529] 12.49385 12.51491 12.53653 12.55871 12.58146 12.60478 12.62866 12.65310
## [537] 12.67811 12.70369 12.72983 12.75653 12.78380 12.81164 12.84003 12.86900
## [545] 12.89852 12.92861 12.95927 12.99049 13.02228 13.05462 13.08754 13.12102
## [553] 13.15506 13.18966 13.22483
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 555)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.59120 12.58835 12.58559 12.58291 12.58031 12.57779 12.57535 12.57298
##   [9] 12.57067 12.56843 12.56625 12.56413 12.56207 12.56005 12.55809 12.55617
##  [17] 12.55429 12.55244 12.55064 12.54886 12.54712 12.54539 12.54369 12.54201
##  [25] 12.54034 12.53869 12.53704 12.53540 12.53376 12.53211 12.53043 12.52874
##  [33] 12.52705 12.52535 12.52365 12.52196 12.52029 12.51864 12.51702 12.51543
##  [41] 12.51388 12.51237 12.51092 12.50952 12.50819 12.50693 12.50574 12.50463
##  [49] 12.50362 12.50269 12.50187 12.50115 12.50054 12.50005 12.49969 12.49945
##  [57] 12.49935 12.49932 12.49928 12.49924 12.49921 12.49917 12.49915 12.49913
##  [65] 12.49913 12.49915 12.49919 12.49925 12.49933 12.49945 12.49960 12.49979
##  [73] 12.50001 12.50028 12.50059 12.50095 12.50137 12.50184 12.50236 12.50295
##  [81] 12.50361 12.50433 12.50512 12.50598 12.50693 12.50795 12.50906 12.51025
##  [89] 12.51153 12.51291 12.51438 12.51595 12.51763 12.51941 12.52130 12.52330
##  [97] 12.52541 12.52765 12.53000 12.53248 12.53509 12.53783 12.54070 12.54371
## [105] 12.54686 12.55015 12.55359 12.55718 12.56092 12.56482 12.56888 12.57310
## [113] 12.57748 12.58173 12.58557 12.58905 12.59223 12.59516 12.59788 12.60046
## [121] 12.60294 12.60537 12.60781 12.61031 12.61292 12.61569 12.61868 12.62193
## [129] 12.62550 12.62944 12.63380 12.63864 12.64400 12.64994 12.65650 12.66502
## [137] 12.67648 12.69044 12.70644 12.72405 12.74282 12.76230 12.78204 12.80161
## [145] 12.82055 12.83842 12.85477 12.86916 12.88114 12.89273 12.90615 12.92128
## [153] 12.93797 12.95610 12.97552 12.99610 13.01770 13.04020 13.06345 13.08732
## [161] 13.11167 13.13637 13.16128 13.18627 13.21121 13.23595 13.26036 13.28431
## [169] 13.30766 13.33028 13.35203 13.37277 13.39237 13.41070 13.42762 13.44299
## [177] 13.45668 13.46856 13.47848 13.48632 13.49193 13.49519 13.49661 13.49683
## [185] 13.49589 13.49383 13.49070 13.48652 13.48135 13.47521 13.46816 13.46023
## [193] 13.45146 13.44190 13.43157 13.42053 13.40882 13.39646 13.38351 13.37000
## [201] 13.35598 13.34148 13.32654 13.31121 13.29553 13.27741 13.25516 13.22941
## [209] 13.20077 13.16986 13.13729 13.10369 13.06966 13.03584 13.00283 12.97125
## [217] 12.94172 12.91487 12.89129 12.86837 12.84318 12.81593 12.78679 12.75598
## [225] 12.72368 12.69009 12.65540 12.61981 12.58351 12.54670 12.50957 12.47232
## [233] 12.43513 12.39822 12.36176 12.32595 12.29099 12.25708 12.22440 12.19316
## [241] 12.16354 12.13574 12.10996 12.08640 12.06523 12.04667 12.03090 12.01767
## [249] 12.00650 11.99721 11.98964 11.98361 11.97896 11.97551 11.97310 11.97155
## [257] 11.97070 11.97038 11.97041 11.97062 11.97086 11.97094 11.97070 11.96996
## [265] 11.96857 11.96634 11.96620 11.97070 11.97910 11.99064 12.00458 12.02018
## [273] 12.03669 12.05336 12.06944 12.08421 12.09690 12.10677 12.11307 12.11507
## [281] 12.11488 12.11508 12.11562 12.11643 12.11745 12.11864 12.11992 12.12125
## [289] 12.12257 12.12381 12.12492 12.12583 12.12651 12.12687 12.12687 12.12645
## [297] 12.12555 12.12411 12.12208 12.11939 12.11599 12.11181 12.10681 12.10092
## [305] 12.09409 12.08625 12.07735 12.06734 12.05456 12.03778 12.01754 11.99439
## [313] 11.96889 11.94157 11.91298 11.88367 11.85419 11.82508 11.79689 11.77017
## [321] 11.74547 11.72332 11.70429 11.68890 11.67354 11.65462 11.63283 11.60882
## [329] 11.58326 11.55683 11.53017 11.50397 11.47888 11.45558 11.43473 11.41699
## [337] 11.40304 11.39353 11.38688 11.38103 11.37600 11.37179 11.36839 11.36582
## [345] 11.36407 11.36315 11.36307 11.36382 11.36542 11.36785 11.37113 11.37526
## [353] 11.38025 11.38609 11.39279 11.40035 11.40878 11.41808 11.42825 11.43930
## [361] 11.45123 11.46404 11.47774 11.49233 11.50781 11.52419 11.54302 11.56561
## [369] 11.59160 11.62063 11.65233 11.68635 11.72232 11.75988 11.79866 11.83830
## [377] 11.87845 11.91873 11.95880 11.99827 12.03679 12.07401 12.10955 12.14305
## [385] 12.17415 12.20250 12.22772 12.25271 12.28029 12.30998 12.34129 12.37376
## [393] 12.40689 12.44021 12.47323 12.50548 12.53648 12.56574 12.59279 12.61714
## [401] 12.63832 12.65776 12.67722 12.69662 12.71592 12.73506 12.75400 12.77267
## [409] 12.79102 12.80901 12.82657 12.84365 12.86020 12.87617 12.89150 12.90614
## [417] 12.92003 12.93312 12.94537 12.95670 12.96708 12.97644 12.98474 12.99192
## [425] 12.99792 13.00270 13.00619 13.00835 13.00913 13.00816 13.00522 13.00047
## [433] 12.99406 12.98613 12.97685 12.96635 12.95480 12.94234 12.92913 12.91531
## [441] 12.90104 12.88647 12.87175 12.85703 12.84246 12.82820 12.81440 12.80120
## [449] 12.78626 12.76751 12.74558 12.72106 12.69456 12.66669 12.63805 12.60926
## [457] 12.58092 12.55364 12.52802 12.50468 12.48421 12.46722 12.45163 12.43501
## [465] 12.41747 12.39914 12.38014 12.36059 12.34061 12.32031 12.29982 12.27927
## [473] 12.25875 12.23841 12.21835 12.19870 12.17958 12.16110 12.14339 12.12657
## [481] 12.11076 12.09607 12.08262 12.07055 12.05996 12.05097 12.04371 12.03830
## [489] 12.03485 12.03349 12.03351 12.03414 12.03540 12.03728 12.03980 12.04298
## [497] 12.04682 12.05132 12.05651 12.06238 12.06896 12.07624 12.08425 12.09298
## [505] 12.10245 12.11267 12.12365 12.13540 12.14793 12.16124 12.17536 12.19028
## [513] 12.20602 12.22259 12.23999 12.25824 12.27735 12.29733 12.31818 12.33992
## [521] 12.36250 12.38585 12.40998 12.43488 12.46056 12.48701 12.51424 12.54225
## [529] 12.57102 12.60057 12.63090 12.66199 12.69387 12.72651 12.75992 12.79411
## [537] 12.82907 12.86480 12.90130 12.93857 12.97662 13.01543 13.05501 13.09537
## [545] 13.13649 13.17838 13.22104 13.26447 13.30866 13.35362 13.39936 13.44585
## [553] 13.49312 13.54115 13.58995
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 555)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.90187 11.90283 11.90383 11.90486 11.90592 11.90698 11.90806 11.90914
##   [9] 11.91021 11.91127 11.91231 11.91333 11.91431 11.91524 11.91613 11.91696
##  [17] 11.91773 11.91843 11.91905 11.91959 11.92003 11.92038 11.92062 11.92074
##  [25] 11.92075 11.92063 11.92037 11.91997 11.91942 11.91872 11.91785 11.91682
##  [33] 11.91560 11.91420 11.91261 11.91081 11.90880 11.90656 11.90411 11.90146
##  [41] 11.89864 11.89566 11.89254 11.88929 11.88594 11.88250 11.87898 11.87541
##  [49] 11.87181 11.86818 11.86455 11.86094 11.85735 11.85382 11.85035 11.84696
##  [57] 11.84368 11.84051 11.83748 11.83460 11.83189 11.82937 11.82705 11.82496
##  [65] 11.82271 11.81994 11.81667 11.81292 11.80873 11.80412 11.79911 11.79372
##  [73] 11.78800 11.78195 11.77560 11.76898 11.76212 11.75504 11.74776 11.74031
##  [81] 11.73272 11.72501 11.71720 11.70933 11.70141 11.69348 11.68555 11.67765
##  [89] 11.66981 11.66206 11.65441 11.64690 11.63954 11.63237 11.62541 11.61868
##  [97] 11.61221 11.60603 11.60016 11.59462 11.58944 11.58465 11.58028 11.57633
## [105] 11.57285 11.56986 11.56738 11.56544 11.56406 11.56326 11.56309 11.56355
## [113] 11.56467 11.56648 11.56901 11.57228 11.57631 11.58113 11.58677 11.59325
## [121] 11.60153 11.61237 11.62556 11.64085 11.65800 11.67678 11.69695 11.71828
## [129] 11.74053 11.76346 11.78683 11.81042 11.83398 11.85728 11.88007 11.90214
## [137] 11.92322 11.94311 11.96154 11.97830 11.99658 12.01929 12.04574 12.07520
## [145] 12.10699 12.14040 12.17473 12.20926 12.24331 12.27617 12.30713 12.33549
## [153] 12.36056 12.38162 12.40101 12.42148 12.44293 12.46526 12.48839 12.51222
## [161] 12.53664 12.56158 12.58692 12.61259 12.63847 12.66449 12.69053 12.71652
## [169] 12.74235 12.76792 12.79316 12.81795 12.84220 12.86583 12.88873 12.91080
## [177] 12.93197 12.95212 12.97117 12.98902 13.00558 13.02075 13.03443 13.04654
## [185] 13.05697 13.06564 13.07244 13.07728 13.08007 13.08131 13.08155 13.08079
## [193] 13.07906 13.07635 13.07268 13.06804 13.06245 13.05592 13.04844 13.04003
## [201] 13.03069 13.02043 13.00926 12.99718 12.98421 12.97034 12.95559 12.93996
## [209] 12.92345 12.90609 12.88487 12.85746 12.82482 12.78790 12.74767 12.70508
## [217] 12.66111 12.61670 12.57281 12.53042 12.49048 12.45394 12.42178 12.39495
## [225] 12.37006 12.34321 12.31459 12.28435 12.25269 12.21977 12.18578 12.15089
## [233] 12.11527 12.07911 12.04258 12.00585 11.96910 11.93252 11.89627 11.86053
## [241] 11.82548 11.79130 11.75816 11.72624 11.69571 11.66676 11.63955 11.61426
## [249] 11.59108 11.57018 11.55173 11.53591 11.52274 11.51200 11.50354 11.49720
## [257] 11.49282 11.49023 11.48929 11.48982 11.49168 11.49470 11.49872 11.50358
## [265] 11.50913 11.51520 11.52163 11.52827 11.53495 11.54152 11.54782 11.55368
## [273] 11.55895 11.56347 11.56708 11.56962 11.57092 11.57084 11.56921 11.56587
## [281] 11.56164 11.55745 11.55328 11.54911 11.54491 11.54067 11.53638 11.53200
## [289] 11.52752 11.52292 11.51818 11.51329 11.50822 11.50295 11.49746 11.49174
## [297] 11.48577 11.47952 11.47298 11.46612 11.45893 11.45139 11.44348 11.43517
## [305] 11.42646 11.41732 11.40773 11.39767 11.38466 11.36672 11.34456 11.31889
## [313] 11.29043 11.25990 11.22801 11.19547 11.16301 11.13133 11.10115 11.07319
## [321] 11.04816 11.02678 11.00976 10.99782 10.98822 10.97790 10.96709 10.95605
## [329] 10.94499 10.93415 10.92378 10.91410 10.90536 10.89779 10.89162 10.88709
## [337] 10.88443 10.88389 10.88539 10.88866 10.89361 10.90017 10.90823 10.91773
## [345] 10.92857 10.94068 10.95395 10.96832 10.98370 11.00000 11.01713 11.03502
## [353] 11.05358 11.07272 11.09236 11.11242 11.13280 11.15344 11.17423 11.19510
## [361] 11.21596 11.23672 11.25731 11.27764 11.29762 11.31718 11.33756 11.35996
## [369] 11.38418 11.41001 11.43726 11.46572 11.49521 11.52550 11.55641 11.58773
## [377] 11.61927 11.65081 11.68217 11.71314 11.74352 11.77311 11.80171 11.82912
## [385] 11.85513 11.87956 11.90219 11.92481 11.94911 11.97475 12.00142 12.02876
## [393] 12.05645 12.08417 12.11157 12.13833 12.16411 12.18858 12.21140 12.23226
## [401] 12.25081 12.26816 12.28560 12.30307 12.32052 12.33791 12.35517 12.37225
## [409] 12.38909 12.40565 12.42187 12.43770 12.45308 12.46796 12.48229 12.49600
## [417] 12.50906 12.52140 12.53297 12.54372 12.55360 12.56254 12.57050 12.57743
## [425] 12.58326 12.58795 12.59144 12.59368 12.59462 12.59367 12.59044 12.58513
## [433] 12.57798 12.56919 12.55899 12.54758 12.53520 12.52204 12.50833 12.49429
## [441] 12.48014 12.46608 12.45234 12.43914 12.42669 12.41520 12.40490 12.39600
## [449] 12.38649 12.37447 12.36035 12.34451 12.32735 12.30927 12.29065 12.27190
## [457] 12.25341 12.23558 12.21879 12.20345 12.18995 12.17868 12.16832 12.15733
## [465] 12.14578 12.13373 12.12124 12.10839 12.09524 12.08186 12.06831 12.05466
## [473] 12.04097 12.02732 12.01376 12.00037 11.98721 11.97435 11.96185 11.94978
## [481] 11.93821 11.92719 11.91681 11.90712 11.89820 11.89010 11.88289 11.87665
## [489] 11.87143 11.86731 11.86387 11.86070 11.85780 11.85521 11.85294 11.85102
## [497] 11.84947 11.84831 11.84756 11.84725 11.84740 11.84803 11.84916 11.85081
## [505] 11.85301 11.85578 11.85914 11.86311 11.86772 11.87298 11.87892 11.88557
## [513] 11.89293 11.90105 11.90993 11.91960 11.93008 11.94140 11.95357 11.96662
## [521] 11.98043 11.99485 12.00989 12.02554 12.04181 12.05870 12.07620 12.09432
## [529] 12.11305 12.13241 12.15238 12.17296 12.19417 12.21599 12.23843 12.26149
## [537] 12.28517 12.30946 12.33438 12.35991 12.38606 12.41283 12.44022 12.46823
## [545] 12.49686 12.52611 12.55598 12.58647 12.61758 12.64931 12.68166 12.71463
## [553] 12.74822 12.78243 12.81727
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")